package com.knife.lov.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knife.common.exception.CommonException;
import com.knife.lov.app.service.CodeService;
import com.knife.lov.domain.entity.KnifeCode;
import com.knife.lov.domain.entity.KnifeCodeValue;
import com.knife.lov.infra.cache.v2.KnifeCodeCache;
import com.knife.lov.infra.cache.v2.KnifeCodeValueCache;
import com.knife.lov.infra.mapper.KnifeCodeMapper;
import com.knife.lov.infra.mapper.KnifeCodeValueMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： 76875
 * @date： 2022/4/15 星期五 17:59
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
public class CodeServiceImpl implements CodeService {

    @Resource
    private KnifeCodeMapper knifeCodeMapper;
    @Resource
    private KnifeCodeCache knifeCodeCache;
    @Resource
    private KnifeCodeValueMapper knifeCodeValueMapper;
    @Resource
    private KnifeCodeValueCache knifeCodeValueCache;

    @Override
    public Page<KnifeCode> selectCodePage(KnifeCode knifeCode, Page<KnifeCode> pageRequest) {
        LambdaQueryWrapper<KnifeCode> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .like(StringUtils.isNotEmpty(knifeCode.getCode()),KnifeCode::getCode,knifeCode.getCode())
                .like(StringUtils.isNotEmpty(knifeCode.getDescription()),KnifeCode::getDescription,knifeCode.getDescription())
                .eq(KnifeCode::getTenantId,knifeCode.getTenantId())
                .orderByDesc(KnifeCode::getCodeId)
        ;
        return knifeCodeMapper.selectPage(pageRequest,knifeCodeLambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeCode saveAndUpdateCode(KnifeCode knifeCode) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeCode> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeCode::getCode,knifeCode.getCode())
                .eq(KnifeCode::getTenantId,knifeCode.getTenantId())
        ;
        KnifeCode db = knifeCodeMapper.selectOne(knifeCodeLambdaQueryWrapper);
        if (Objects.isNull(db)){
            db = new KnifeCode();
        }
        // 替换数据
        db.setCode(knifeCode.getCode());
        db.setDescription(knifeCode.getDescription());
        db.setTenantId(knifeCode.getTenantId());
        db.setEnabledFlag(knifeCode.getEnabledFlag());
        if (Objects.isNull(db.getCodeId())){
            knifeCodeMapper.insert(db);
        }else {
            knifeCodeMapper.updateById(db);
        }
        // 保存缓存
        knifeCodeCache.setCache(db);
        return db;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeCode deleteCode(KnifeCode knifeCode) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeCode> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeCode::getCode,knifeCode.getCode())
                .eq(KnifeCode::getTenantId,knifeCode.getTenantId())
        ;
        KnifeCode db = knifeCodeMapper.selectOne(knifeCodeLambdaQueryWrapper);
        if (Objects.isNull(db)){
            throw new CommonException("删除的数据不存在");
        }
        knifeCodeMapper.deleteById(db);
        knifeCodeCache.deleteCache(db);
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeCodeValue> knifeCodeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeValueLambdaQueryWrapper
                .eq(KnifeCodeValue::getCodeId, db.getCodeId())
        ;
        List<KnifeCodeValue> dbValueList = knifeCodeValueMapper.selectList(knifeCodeValueLambdaQueryWrapper);
        dbValueList.forEach(item->{
            knifeCodeValueMapper.deleteById(item);
            knifeCodeValueCache.deleteCache(item);
        });
        return db;
    }
}
